package com.honglang.aop.impl;


import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.WebApplicationContext;

import com.honglang.module.article.service.BlogArticleManager;
import com.honglang.search.lucene.SearchEngineer;
import com.honglang.struts2.action.BaseAction;

@Aspect
public class SearchIndexEngineerAspect {
	private static final Logger logger = Logger.getLogger(FileUploadAspect.class);
	
	private SearchEngineer searchEngineer;
	
	
	@Pointcut("execution(* com.honglang..action.*.*(..))")
	protected void searchEngineerPointCut(){};
	
	@After("searchEngineerPointCut() && " +"@annotation(com.honglang.aop.impl.UpdateSearchIndexPointCut)")
	public void updateSearchIndex(JoinPoint jp){
		logger.info("Updating search index file...");
		BaseAction baseAction = (BaseAction)jp.getTarget();
		HttpServletRequest request =  baseAction.getRequest();
		WebApplicationContext  webApplicationContext = baseAction.getWebApplicationContext();
		BlogArticleManager blogArticleManager = (BlogArticleManager)webApplicationContext.getBean("blogArticleManager");
		String indexDirectory = request.getSession().getServletContext().getRealPath("/") + "search\\index";
		this.getSearchEngineer().createIndexDir(indexDirectory, blogArticleManager.getAllBlogArticle(), true);
		logger.info("Search index file has been updated!");
	}

	
	
	public SearchEngineer getSearchEngineer() {
		return searchEngineer;
	}

	public void setSearchEngineer(SearchEngineer searchEngineer) {
		this.searchEngineer = searchEngineer;
	}
	
	
	
}
